home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / comm / tcp / amitcptelnetf.lha / amitcp_telnet+ftp / ftp / domacro.c < prev    next >
C/C++ Source or Header  |  1993-06-07  |  3KB  |  147 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)domacro.c    1.6 (Berkeley) 2/28/89";
  20. #endif /* not lint */
  21.  
  22. #include "ftp_var.h"
  23.  
  24. #include <signal.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <ctype.h>
  28. #if !defined(__hpux)
  29. //#include <sys/ttychars.h>
  30. #endif /* __hpux */
  31.  
  32. domacro(argc, argv)
  33.     int argc;
  34.     char *argv[];
  35. {
  36.     register int i, j;
  37.     register char *cp1, *cp2;
  38.     int count = 2, loopflg = 0;
  39.     char line2[200];
  40.     extern char **glob(), *globerr;
  41.     struct cmd *getcmd(), *c;
  42.     extern struct cmd cmdtab[];
  43.  
  44.     if (argc < 2) {
  45.         (void) strcat(line, " ");
  46.         printf("(macro name) ");
  47.         (void) gets(&line[strlen(line)]);
  48.         makeargv();
  49.         argc = margc;
  50.         argv = margv;
  51.     }
  52.     if (argc < 2) {
  53.         printf("Usage: %s macro_name.\n", argv[0]);
  54.         code = -1;
  55.         return;
  56.     }
  57.     for (i = 0; i < macnum; ++i) {
  58.         if (!strncmp(argv[1], macros[i].mac_name, 9)) {
  59.             break;
  60.         }
  61.     }
  62.     if (i == macnum) {
  63.         printf("'%s' macro not found.\n", argv[1]);
  64.         code = -1;
  65.         return;
  66.     }
  67.     (void) strcpy(line2, line);
  68. TOP:
  69.     cp1 = macros[i].mac_start;
  70.     while (cp1 != macros[i].mac_end) {
  71.         while (isspace(*cp1)) {
  72.             cp1++;
  73.         }
  74.         cp2 = line;
  75.         while (*cp1 != '\0') {
  76.               switch(*cp1) {
  77.                    case '\\':
  78.                  *cp2++ = *++cp1;
  79.                  break;
  80.                 case '$':
  81.                  if (isdigit(*(cp1+1))) {
  82.                     j = 0;
  83.                     while (isdigit(*++cp1)) {
  84.                       j = 10*j +  *cp1 - '0';
  85.                     }
  86.                     cp1--;
  87.                     if (argc - 2 >= j) {
  88.                     (void) strcpy(cp2, argv[j+1]);
  89.                     cp2 += strlen(argv[j+1]);
  90.                     }
  91.                     break;
  92.                  }
  93.                  if (*(cp1+1) == 'i') {
  94.                     loopflg = 1;
  95.                     cp1++;
  96.                     if (count < argc) {
  97.                        (void) strcpy(cp2, argv[count]);
  98.                        cp2 += strlen(argv[count]);
  99.                     }
  100.                     break;
  101.                 }
  102.                 /* intentional drop through */
  103.                 default:
  104.                 *cp2++ = *cp1;
  105.                 break;
  106.               }
  107.               if (*cp1 != '\0') {
  108.              cp1++;
  109.               }
  110.         }
  111.         *cp2 = '\0';
  112.         makeargv();
  113.         c = getcmd(margv[0]);
  114.         if (c == (struct cmd *)-1) {
  115.             printf("?Ambiguous command\n");
  116.             code = -1;
  117.         }
  118.         else if (c == 0) {
  119.             printf("?Invalid command\n");
  120.             code = -1;
  121.         }
  122.         else if (c->c_conn && !connected) {
  123.             printf("Not connected.\n");
  124.             code = -1;
  125.         }
  126.         else {
  127.             if (verbose) {
  128.                 printf("%s\n",line);
  129.             }
  130.             (*c->c_handler)(margc, margv);
  131.             if (bell && c->c_bell) {
  132.                 (void) putchar('\007');
  133.             }
  134.             (void) strcpy(line, line2);
  135.             makeargv();
  136.             argc = margc;
  137.             argv = margv;
  138.         }
  139.         if (cp1 != macros[i].mac_end) {
  140.             cp1++;
  141.         }
  142.     }
  143.     if (loopflg && ++count < argc) {
  144.         goto TOP;
  145.     }
  146. }
  147.